package com.jeecg.mvc.base;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeecg.mvc.init.MvcXmlPraseUtil;
import com.jeecg.mvc.init.XmlBean;
import com.jeecg.mvc.util.FullFormUtil;

/**
 * Servlet implementation class MvcBaseServlet
 */
@WebServlet("/MvcBaseServlet")
public class MvcBaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MvcBaseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String url = request.getRequestURI();
		String actionPath = url.substring(url.lastIndexOf("/"), url.length());
		Map<String, Object> configMap = (Map<String, Object>) this.getServletContext().getAttribute("configMap");
		XmlBean xmlBean = (XmlBean) configMap.get(actionPath);
		MvcBaseForm form=FullFormUtil.full(xmlBean.getFormClass(), request);
		MvcBaseAction action= null;
		try {
			Class actionClass=Class.forName(xmlBean.getActionClass());
			action=(MvcBaseAction) actionClass.newInstance();
			String urlPath=action.execute(request, form, xmlBean.getForwardMap());
			RequestDispatcher dis=request.getRequestDispatcher(urlPath);
			dis.forward(request, response);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		     
		
	}

}
